﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace renwu
{
    /// <summary>
    /// 任务：Framework.4.0出现
    /// System.Threading.Tasks
    /// Task.Run(()=>Console.WriteLine("启动任务"));//Ation委托实现
    /// Task默认使用线程池中的线程.
    /// Wait方法
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //Task taskDemo = Task.Run(() =>
            //{
            //    Thread.Sleep(2000);
            //    Console.WriteLine("我是延时两秒加载的线程");
            //});

            //Console.WriteLine(taskDemo.IsCompleted);//检查任务是否完成/结束
            //taskDemo.Wait();//中断，直到任务结束
            //Console.WriteLine(taskDemo.IsCanceled);

            ////计算30万以内的素数个数
            ///

            //Task<int> taskPrime = Task.Run(() =>
            // Enumerable.Range(2, 300000)
            // .Count(prop => Enumerable.Range(2, (int)Math.Sqrt(prop) - 1)
            // .All(i => prop % i > 0)));

            //var awaitObj = taskPrime.GetAwaiter();
            //awaitObj.OnCompleted(() =>
            //{
            //    int count = awaitObj.GetResult();
            //    Console.WriteLine($"中断后输出：{count}");

            //});
            //Console.WriteLine($"30万以内的素数{taskPrime.Result}"); ;


            bool a;
            int n = 0;
            for (int i = 2; i <= 300000; i++)
            {
                a = true;
                for (int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        a = false;
                        break;
                    }
                }
                if (a)
                {
                    n++;
                }
            }
            Console.WriteLine($"30万以内的素数个数有：{n}");
            Console.ReadKey();
        }
    }
}
